//
// Document.cs
//
// Author: 
//  Fredy Ramirez <framirez@pacificcoolwave.com>
//
// Copyright (C) 2011 Fredy Ramirez (http://www.pacificcoolwave.com)
//
//  Creation date: Aug 10, 2011
//  Last modification: 

/*
The MIT License

Copyright (C) 2011 Fredy Ramirez (http://www.pacificcoolwave.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
 */

using System;
using Gtk;
using System.Collections.Generic;
using Mono.Addins;

namespace Pcw.PcwTextEditor.PcwDocument
{
	/// <summary>
	/// A document representation.
	/// </summary>	
	public class Document : Notebook
	{
		private IDocumentType doc_info;
		
		private static int new_count = 0; // for automatic new file names

		private TabLabel tab_label;
		
		public delegate void DocumentEventHandler(Document sender);
		public event DocumentEventHandler DocumentCreate;
		public event DocumentEventHandler DocumentLoad;
		public event DocumentEventHandler DocumentSave;
		public event DocumentEventHandler DocumentClose;
		public event DocumentEventHandler DocumentChange;
		public event DocumentEventHandler DocumentSelect;
		
		private Notebook main_ntb;
		private List<DocumentView> views = new List<DocumentView>();
		private PcwTextView view_text;		
		
		public IDocumentType DocInfo {
			get {
				return doc_info;
			}
		}
		
		/// <summary>
		/// <p>The PcwTextView object.</p> 
		/// <p>It is the main view where most main routines are (OpenFile, SaveFile, etc).</p>
		/// </summary>
		public PcwTextView ViewText
		{
			get {return view_text;}
		}
		
		/// <summary>
		/// The label of tab widget.
		/// </summary>
		public TabLabel TabLabel
		{
			get {return tab_label;}
		}
		
		public string Text {
			get{
				return view_text.Buffer.Text;
			}
			set {
				view_text.Buffer.Text = value;
			}
		}
		
		
		/// <summary>
		/// Default constructor.
		/// </summary>
		/// <param name="mainNotebook">
		///   A <see cref="Gtk.Notebook"/> object where document tab is added
		/// </param>
		public Document (Notebook mainNotebook, IDocumentType docInfo) : base()
		{
			main_ntb = mainNotebook;
			doc_info = docInfo;
			doc_info.DocumentTypeClose += onDocumentClose;
			doc_info.DocumentTypeCreate += onDocumentCreate;
			doc_info.DocumentTypeLoad += onDocumentLoad;
			doc_info.DocumentTypeSave += onDocumentSave;
			
			BuildControl();
		}

		
		
		/// <summary>
		/// Add a view.
		/// </summary>
		/// <param name="viewObject">
		///   A <see cref="DocumentView"/> object to be added.
		/// </param>
		public void AddView(DocumentView viewObject)
		{
			views.Add(viewObject);
			AppendPage(viewObject, new Label(viewObject.Label));
		}
		
		/// <summary>
		/// Set the document focus in main window.
		/// </summary>
		public new void GrabFocus()
		{
			CurrentPage = 0;
			view_text.GrabFocus();
		}
		
		public static string NextNewFileName()
		{
			new_count++;
			return AddinManager.CurrentLocalizer.GetString("Unsaved-{0}", new_count.ToString());
		}
		
		private void BuildControl()
		{
			tab_label = new TabLabel(doc_info.NameWithExtension);	
			tab_label.ButtonClose.Clicked += onBtnCloseClick;
		
			TabPos = PositionType.Bottom;
			Scrollable = true;
			EnablePopup = false;
			
			// Create PcwTextView object 'Text'
			
			view_text = new PcwTextView("Text", AddinManager.CurrentLocalizer.GetString("Text"), new TextView(), this);
			view_text.DocumentViewChange += onDocumentChanged;
			view_text.DocumentViewSelect += onDocumentSelect;
			
			AddView(view_text);
		
			doc_info.Buffer = view_text.Buffer;
			view_text.Buffer.Text = doc_info.Text;
		}
		
		private void onBtnCloseClick(object obj, EventArgs args)
		{
			// Set the current page
			main_ntb.CurrentPage = main_ntb.PageNum(this);
			
			if(DocumentClose != null)
				DocumentClose(this);
		}
		
		private void onDocumentClose(IDocumentType sender)
		{
			if(DocumentClose != null)
				DocumentClose(this);
		}
		
		private void onDocumentChanged(DocumentView obj)
		{
			if (DocumentChange != null)
				DocumentChange(this);
		}
		
		private void onDocumentCreate(IDocumentType sender)
		{
			if (DocumentCreate != null)
				DocumentCreate(this);
		}

		private void onDocumentLoad(IDocumentType sender)
		{
			if (DocumentLoad != null)
				DocumentLoad(this);
		}
		
		private void onDocumentSave(IDocumentType sender)
		{
			TabLabel.Label = doc_info.NameWithExtension;
			if (DocumentSave != null)
				DocumentSave(this);
		}
		
		private void onDocumentSelect(DocumentView obj)
		{
			if (DocumentSelect != null)
				DocumentSelect(this);
		}
	}
	
	public class TabLabel : HBox
	{
		private Button btn_close;
		private Label tab_label;

		public string Label 
		{
			set {
				tab_label.Text = value;
			}
		}
		
		public Button ButtonClose 
		{
			get {
				return btn_close;
			}
		}		
		
		public TabLabel(string tabLabel) : base(false, 0)
		{			
			tab_label = new Label(tabLabel);
			
			Image img = new Image(Stock.Cancel, IconSize.Menu);
			btn_close = new Button();
			btn_close.Image = img;
			btn_close.Relief = ReliefStyle.None;
			
			PackStart(tab_label, true, false, 0);
			PackEnd(btn_close,false,false,0);
			
			ShowAll();			
		}
	}
	
	public delegate void DocumentTypeEventHandler(IDocumentType sender);
}

